/* CoralCube | Massively Multiplayer Online Role-Playing Game(MMORPG) Emulator.
|* 
|* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
|*               2008-2010 CoralCube <http://www.CoralCube.com/>
|*
|* This program is free software. You can redistribute it and/or modify it under the terms of 
|* the GNU General Public License as published by the Free Software Foundation, either 
|* version 2 of the License.
|*
|* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, 
|* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
|* See the GNU General Public License for more details.
|*
|* You should have received a copy of the GNU General Public License along with this program. 
|* If not, see <http://www.gnu.org/licenses/>.
|*/

/* ScriptData
Name: event_commandscript
%Complete: 100
Comment: All event related commands
Category: commandscripts
EndScriptData */

#include "ScriptMgr.h"
#include "GameEventMgr.h"
#include "Chat.h"

class event_commandscript : public CommandScript
{
    public:
        event_commandscript() : CommandScript("event_commandscript") { }

        ChatCommand* GetCommands() const
        {
            static ChatCommand eventCommandTable[] =
            {
                { "activelist",     SEC_GAMEMASTER,     true,  &HandleEventActiveListCommand,     "", NULL },
                { "start",          SEC_GAMEMASTER,     true,  &HandleEventStartCommand,          "", NULL },
                { "stop",           SEC_GAMEMASTER,     true,  &HandleEventStopCommand,           "", NULL },
                { "",               SEC_GAMEMASTER,     true,  &HandleEventInfoCommand,           "", NULL },
                { NULL,             0,                  false, NULL,                              "", NULL }
            };
            static ChatCommand commandTable[] =
            {
                { "event",          SEC_GAMEMASTER,     false, NULL,                  "", eventCommandTable },
                { NULL,             0,                  false, NULL,                               "", NULL }
            };
            return commandTable;
        }

        static bool HandleEventActiveListCommand(ChatHandler* handler, const char* /*args*/)
        {
            uint32 counter = 0;

            GameEventMgr::GameEventDataMap const& events = sGameEventMgr.GetEventMap();
            GameEventMgr::ActiveEvents const& activeEvents = sGameEventMgr.GetActiveEventList();

            char const* active = handler->GetCoreString(LANG_ACTIVE);

            for (GameEventMgr::ActiveEvents::const_iterator itr = activeEvents.begin(); itr != activeEvents.end(); ++itr)
            {
                uint32 event_id = *itr;
                GameEventData const& eventData = events[event_id];

                if (handler->GetSession())
                    handler->PSendSysMessage(LANG_EVENT_ENTRY_LIST_CHAT,event_id,event_id,eventData.description.c_str(),active);
                else
                    handler->PSendSysMessage(LANG_EVENT_ENTRY_LIST_CONSOLE,event_id,eventData.description.c_str(),active);

                ++counter;
            }

            if (counter == 0)
                handler->SendSysMessage(LANG_NOEVENTFOUND);
                handler->SetSentErrorMessage(true);

            return true;
        }

        static bool HandleEventInfoCommand(ChatHandler* handler, const char* args)
        {
            if (!*args)
                return false;

            // id or [name] Shift-click form |color|Hgameevent:id|h[name]|h|r
            char* cId =  handler->extractKeyFromLink((char*)args,"Hgameevent");
            if (!cId)
                return false;

            uint32 event_id = atoi(cId);

            GameEventMgr::GameEventDataMap const& events = sGameEventMgr.GetEventMap();

            if (event_id >=events.size())
            {
                handler->SendSysMessage(LANG_EVENT_NOT_EXIST);
                handler->SetSentErrorMessage(true);
                return false;
            }

            GameEventData const& eventData = events[event_id];
            if (!eventData.isValid())
            {
                handler->SendSysMessage(LANG_EVENT_NOT_EXIST);
                handler->SetSentErrorMessage(true);
                return false;
            }

            GameEventMgr::ActiveEvents const& activeEvents = sGameEventMgr.GetActiveEventList();
            bool active = activeEvents.find(event_id) != activeEvents.end();
            char const* activeStr = active ? handler->GetCoreString(LANG_ACTIVE) : "";

            std::string startTimeStr = TimeToTimestampStr(eventData.start);
            std::string endTimeStr = TimeToTimestampStr(eventData.end);

            uint32 delay = sGameEventMgr.NextCheck(event_id);
            time_t nextTime = time(NULL)+delay;
            std::string nextStr = nextTime >= eventData.start && nextTime < eventData.end ? TimeToTimestampStr(time(NULL)+delay) : "-";

            std::string occurenceStr = secsToTimeString(eventData.occurence * MINUTE);
            std::string lengthStr = secsToTimeString(eventData.length * MINUTE);

            handler->PSendSysMessage(LANG_EVENT_INFO,event_id,eventData.description.c_str(),activeStr,
                startTimeStr.c_str(),endTimeStr.c_str(),occurenceStr.c_str(),lengthStr.c_str(),
                nextStr.c_str());
            return true;
        }

        static bool HandleEventStartCommand(ChatHandler* handler, const char* args)
        {
            if (!*args)
                return false;

            // id or [name] Shift-click form |color|Hgameevent:id|h[name]|h|r
            char* cId =  handler->extractKeyFromLink((char*)args,"Hgameevent");
            if (!cId)
                return false;

            int32 event_id = atoi(cId);

            GameEventMgr::GameEventDataMap const& events = sGameEventMgr.GetEventMap();

            if (event_id < 1 || uint32(event_id) >= events.size())
            {
                handler->SendSysMessage(LANG_EVENT_NOT_EXIST);
                handler->SetSentErrorMessage(true);
                return false;
            }

            GameEventData const& eventData = events[event_id];
            if (!eventData.isValid())
            {
                handler->SendSysMessage(LANG_EVENT_NOT_EXIST);
                handler->SetSentErrorMessage(true);
                return false;
            }

            GameEventMgr::ActiveEvents const& activeEvents = sGameEventMgr.GetActiveEventList();
            if (activeEvents.find(event_id) != activeEvents.end())
            {
                handler->PSendSysMessage(LANG_EVENT_ALREADY_ACTIVE,event_id);
                handler->SetSentErrorMessage(true);
                return false;
            }

            sGameEventMgr.StartEvent(event_id,true);
            return true;
        }

        static bool HandleEventStopCommand(ChatHandler* handler, const char* args)
        {
            if (!*args)
                return false;

            // id or [name] Shift-click form |color|Hgameevent:id|h[name]|h|r
            char* cId =  handler->extractKeyFromLink((char*)args,"Hgameevent");
            if (!cId)
                return false;

            int32 event_id = atoi(cId);

            GameEventMgr::GameEventDataMap const& events = sGameEventMgr.GetEventMap();

            if (event_id < 1 || uint32(event_id) >= events.size())
            {
                handler->SendSysMessage(LANG_EVENT_NOT_EXIST);
                handler->SetSentErrorMessage(true);
                return false;
            }

            GameEventData const& eventData = events[event_id];
            if (!eventData.isValid())
            {
                handler->SendSysMessage(LANG_EVENT_NOT_EXIST);
                handler->SetSentErrorMessage(true);
                return false;
            }

            GameEventMgr::ActiveEvents const& activeEvents = sGameEventMgr.GetActiveEventList();

            if (activeEvents.find(event_id) == activeEvents.end())
            {
                handler->PSendSysMessage(LANG_EVENT_NOT_ACTIVE,event_id);
                handler->SetSentErrorMessage(true);
                return false;
            }

            sGameEventMgr.StopEvent(event_id,true);
            return true;
        }
};

void AddSC_event_commandscript()
{
    new event_commandscript();
}